// 当浏览器安装时 设置状态
chrome.runtime.onInstalled.addListener(() => {
    chrome.action.setBadgeText({
        text: 'OFF'
    })
})
let scriptExecuted = false;
// 当用户点击插件图标时
chrome.action.onClicked.addListener(async (tab) => {
    // 获取当前状态
    const prevState = await chrome.action.getBadgeText({
        tabId: tab.id
    })
    // 设置状态
    const nextState = prevState === 'ON' ? 'OFF' : 'ON'
    //更新我们插件的状态
    chrome.action.setBadgeText({
        text: nextState,
        tabId: tab.id
    })
    // 如果当前插件状态是ON
    if (!scriptExecuted) {
        if (nextState === 'ON') {
            await chrome.scripting.executeScript({
                target: {
                    tabId: tab.id
                },
                files: ['content-script.js']
            })
            scriptExecuted = true;
        } else {

        }
    }
    await chrome.tabs.sendMessage(tab.id, {
        extension: nextState
    })
})